package com.niit.dao;

import model.User;
import util.DataBaseUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class UserDao {
    /**
     * 添加用户
     * @param user 用户对象
     */
    public void saveUser(User user) throws SQLException {
        Connection conn = DataBaseUtil.getConnection();
        String SQL = "insert into tb_user(username,password,tel,user_emai) values(?,?,?,?)";
        try{
            PreparedStatement ps = conn.prepareStatement(SQL);
            ps.setString(1,user.getUsername());
            ps.setString(2,user.getPassword());
            ps.setString(3,user.getTel());
            ps.setString(3,user.getEmail());
            ps.executeUpdate();
            ps.close();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        DataBaseUtil.closeConnection(conn);

    }
    /**
     * 用户登录
     * @param username 用户名
     * @param password 密码
     * @return 用户对象
     */
    public User login(String username, String password){
        User user = null;

        Connection conn = DataBaseUtil.getConnection();

        String sql = "select * from tb_user where username = ? and password = ?";
        try {

            PreparedStatement ps = conn.prepareStatement(sql);

            ps.setString(1, username);
            ps.setString(2, password);

            ResultSet rs = ps.executeQuery();
            if(rs.next()){
                user = new User();
                user.setUsername(rs.getString("username"));
                user.setPassword(rs.getString("password"));
                user.setTel(rs.getString("tel"));
                user.setEmail(rs.getString("email"));
            }
            rs.close();
            ps.close();
        } catch (Exception e) {
            e.printStackTrace();
        }finally{

            DataBaseUtil.closeConnection(conn);
        }
        return user;
    }
    /**
     * 判断用户名在数据库中是否存在
     * @param username 用户名
     * @return 布尔值
     */
    public boolean userIsExist(String username){

        Connection conn = DataBaseUtil.getConnection();

        String sql = "select * from tb_user where username = ?";
        try {

            PreparedStatement ps = conn.prepareStatement(sql);

            ps.setString(1, username);

            ResultSet rs = ps.executeQuery();

            if(!rs.next()){

                return true;
            }

            rs.close();

            ps.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally{

            DataBaseUtil.closeConnection(conn);
        }
        return false;
    }
    /**
     * 判断用户名在数据库中是否存在
     * @param username 用户名
     * @return Email 字符串
     */
    public String getEmail(String username){
        String Email = null;
        Connection conn = DataBaseUtil.getConnection();
        String sql = "select * from users where user_name=?;";
        try{
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1,username);
            ResultSet rs = ps.executeQuery();
            if(rs!=null&&rs.next()){
                Email = rs.getString("user_email");
            }
            ps.close();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            DataBaseUtil.closeConnection(conn);
        }
        return Email;
    }
}
